<template>
  <a-spin :spinning="confirmLoading" style="background: #fff">
    <j-form-container :disabled="false" >
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息">
            <a-row :gutter="48">
              <a-col :span="8" :hidden="true">
                <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bpmBizTitle">
                  <a-input
                    :disabled="formDisabled"
                    v-model="model.bpmBizTitle"
                    placeholder="请输入标题"
                    :max-length="255"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNo">
                  <a-input :disabled="formDisabled" v-model="model.billNo" placeholder="请输入单据编号" disabled ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposer">
                  <select-user-by-dep
                    :disabled="formDisabled"
                    :multi="false"
                    v-model="model.proposer"
                    disabled></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择申请时间"
                    v-model="model.createTime"
                    :show-time="true"
                    date-format="YYYY-MM-DD HH:mm:ss"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposerDeptId">
                  <j-select-depart :disabled="formDisabled" v-model="model.proposerDeptId" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="关联项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
                  <a-input
                    v-if="formDisabled"
                    readOnly
                    @click.prevent="goProjectDetail(model)"
                    v-model="model.projectName"
                    style="color: #1890FF; cursor: pointer;"
                    placeholder="请选择项目"></a-input>
                  <template v-else>
                    <select-project-input v-model="model.projectId" placeholder="请输入选择项目" @input="queryBillByProject" :disabled="formDisabled" />
                  </template>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="applicant">
                  <select-Company-input :disabled="formDisabled" v-model="model.applicant" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="费用所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectBusiTeamId">
                  <select-team-input
                    :disabled="formDisabled"
                    v-model="model.projectBusiTeamId"
                    :tdlx="3"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="业绩归属年度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectYear">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.projectYear"
                    dictCode="yjgsnd"
                    placeholder="请选择业绩归属年度"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="本项目累计支付金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectPaidTotalAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    :min="0"
                    v-model="model.projectPaidTotalAmount"
                    placeholder="请输入本项目累计支付金额"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="本项目累计支付比例（%）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectPaidRate">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    :min="0"
                    v-model="model.projectPaidRate"
                    placeholder="请输入本项目累计支付比例"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="是否垫付工程款" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isAdvance">
                  <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.isAdvance" dictCode="YES_OR_NO" placeholder="请选择是否垫付工程款" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="采购类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectPurchaseCategories">
                  <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.projectPurchaseCategories" dictCode="purchase_categories" placeholder="请选择采购类别" />
                </a-form-model-item>
              </a-col>

              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                  <a-textarea :disabled="formDisabled" v-model="model.remark" rows="4" placeholder="请输入备注" :max-length="500"/>
                </a-form-model-item>
              </a-col>
              <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col> -->
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="建设方信息" >
            <a-row :gutter="48">
              <!-- <a-col :span="8">
                <a-form-model-item label="是否关联收款合同" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ownerInfo.isRelConllectionContract">
                  <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.ownerInfo.isRelConllectionContract" dictCode="is_rel_conllection_contract" placeholder="请输入是否关联收款合同" @input="radioChange(1)" />
                </a-form-model-item>
              </a-col> -->
              <template v-if="model.ownerInfo.isRelConllectionContract=='1'">
                <a-col :span="8">
                  <a-form-model-item label="关联收款合同" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ownerInfo.conllectionContractId">
                    <select-contract-manager-input
                      showName="1"
                      :muitFlag="true"
                      :disabled="formDisabled || !model.projectId"
                      v-model="model.ownerInfo.conllectionContractId"
                      :placeholder="model.projectId ? '请选择关联收款合同' : '请先选择关联项目'"
                      htlx="1"
                      :projectId="model.projectId"
                      @back="getCollectContractInfo"/>
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item label="建设单位合同金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ownerInfo.ownerContractAmount">
                    <a-input-number
                      :disabled="formDisabled"
                      :max="99999999"
                      :min="0"
                      :precision="2"
                      v-model="model.ownerInfo.ownerContractAmount"
                      placeholder="请输入建设单位合同金额"
                      style="width: 100%"
                      disabled/>
                  </a-form-model-item>
                </a-col>
              </template>
              <a-col :span="8">
                <a-form-model-item label="建设单位调整后合同金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ownerInfo.ownerAdjustedContractAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    @change="inputReturnedAmount"
                    v-model="model.ownerInfo.ownerAdjustedContractAmount"
                    placeholder="请输入建设单位调整后合同金额"
                    style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="建设单位已回款金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ownerInfo.ownerReturnedAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="model.ownerInfo.ownerAdjustedContractAmount"
                    :precision="2"
                    :min="0"
                    @change="inputReturnedAmount"
                    v-model="model.ownerInfo.ownerReturnedAmount"
                    placeholder="请输入建设单位已回款金额"
                    style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="建设单位已回款比例%" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ownerInfo.ownerReturnedRate">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    v-model="model.ownerInfo.ownerReturnedRate"
                    placeholder="请输入建设单位已回款比例%"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="3" header="施工方信息" >
            <a-row :gutter="48">
              <!-- <a-col :span="8">
                <a-form-model-item label="是否关联付款合同" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.isRelPaymentContract">
                  <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.constructInfo.isRelPaymentContract"  dictCode="is_rel_payment_contract" placeholder="请输入是否关联付款合同"  @input="radioChange(2)"/>
                </a-form-model-item>
              </a-col> -->
              <template v-if="model.constructInfo.isRelPaymentContract=='1'">
                <a-col :span="8">
                  <a-form-model-item label="关联付款合同" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.paymentContractId">
                    <select-contract-manager-input
                      showName="1"
                      :muitFlag="true"
                      :disabled="formDisabled || !model.projectId"
                      v-model="model.constructInfo.paymentContractId"
                      :placeholder="model.projectId ? '请选择关联付款合同' : '请先选择关联项目'"
                      htlx="2"
                      :projectId="model.projectId"
                      @back="getContractInfo"/>
                  </a-form-model-item>
                </a-col>
                <a-col :span="8">
                  <a-form-model-item label="施工方合同金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.constructionContractAmount">
                    <a-input-number
                      :disabled="formDisabled"
                      :max="99999999"
                      :min="0"
                      :precision="2"
                      v-model="model.constructInfo.constructionContractAmount"
                      placeholder="请输入施工方合同金额"
                      style="width: 100%"
                      disabled/>
                  </a-form-model-item>
                </a-col>
              </template>
              <a-col :span="8">
                <a-form-model-item label="施工方调整后合同金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.constructionAdjustedContractAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    v-model="model.constructInfo.constructionAdjustedContractAmount"
                    @change="inputAmount"
                    placeholder="请输入施工方调整后合同金额"
                    style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="已开票累计金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.invoicedTotalAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    v-model="model.constructInfo.invoicedTotalAmount"
                    placeholder="请输入已开票累计金额"
                    style="width: 100%"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="已支付金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.paidAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    v-model="model.constructInfo.paidAmount"
                    placeholder="请输入已支付金额"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="发票类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.isSpecialInvoice">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    @input="isSpecial"
                    v-model="model.constructInfo.isSpecialInvoice"
                    dictCode="invoice_type"
                    placeholder="请选择发票类型" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="报销金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.reimbursementAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :precision="2"
                    :max="model.constructInfo.constructionAdjustedContractAmount"
                    :min="0"
                    v-model="model.constructInfo.reimbursementAmount"
                    @change="inputTaxAmount"
                    placeholder="请输入报销金额"
                    style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="税费金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.taxAmount">
                  <a-input-number
                    :disabled="formDisabled|| isSpecialDisabled"
                    :precision="2"
                    :max="model.constructInfo.reimbursementAmount"
                    :min="0"
                    @change="inputTaxAmount"
                    v-model="model.constructInfo.taxAmount"
                    placeholder="请输入税费金额"
                    style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="未税金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.untaxedAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    v-model="model.constructInfo.untaxedAmount"
                    @change="inputTaxAmount"
                    placeholder="请输入未税金额"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="本次支付金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.currentPaymentAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :precision="2"
                    :max="model.constructInfo.constructionAdjustedContractAmount"
                    :min="0"
                    v-model="model.constructInfo.currentPaymentAmount"
                    @change="inputAmount"
                    placeholder="请输入本次支付金额"
                    style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="本次支付比例（%）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.currentPaymentRate">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    v-model="model.constructInfo.currentPaymentRate"
                    placeholder="请输入本次支付比例"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="累计支付金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.paidTotalAmount">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    v-model="model.constructInfo.paidTotalAmount"
                    placeholder="请输入累计支付金额"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="累计支付比例（%）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.paidTotalRate">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    :precision="2"
                    v-model="model.constructInfo.paidTotalRate"
                    placeholder="请输入累计支付比例"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="付款方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.payMode">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.constructInfo.payMode"
                    dictCode="pay_mode"
                    placeholder="请选择付款方式"
                    @input="radioModeChange" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="收款单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.payeeName">
                  <!-- <select-customer-input :disabled="formDisabled" commercialType="2" v-model="model.constructInfo.payeeName" @back="getCustomerInfo" /> -->
                  <select-supplier-input :disabled="formDisabled" placeholder="请选择收款单位" v-model="model.constructInfo.payeeName" @back="getCustomerInfo" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-if="model.constructInfo.payMode=='1'">
                <a-form-model-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.bankName">
                  <a-input :disabled="formDisabled" v-model="model.constructInfo.bankName" placeholder="请输入开户行" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8" v-if="model.constructInfo.payMode=='1'">
                <a-form-model-item label="账户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.accountNo">
                  <a-input :disabled="formDisabled" :max-length="32" v-model="model.constructInfo.accountNo" placeholder="请输入账户" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructInfo.remark">
                  <a-textarea :disabled="formDisabled" v-model="model.constructInfo.remark" rows="4" placeholder="请输入备注" :max-length="500"/>
                </a-form-model-item>
              </a-col>

            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="4" header="附件信息">
            <a-row :gutter="48">
              <a-col :span="24">
                <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachments">
                  <j-upload :disabled="formDisabled" v-model="model.attachments"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
      </a-form-model>
      <common-flow-list v-if="formBpm && model.bpmStatus !== '1'" :formData="formData" style="margin-top: 10px"></common-flow-list>
      <!-- 项目详情开始 -->
      <erp-project-report-modal ref="projectReport"></erp-project-report-modal>
      <!-- 项目详情结束 -->
    </j-form-container>
    <a-row :gutter="48">
      <a-col :span="24" style="text-align: center;margin-top: 15px">
        <a-button type="primary" v-if="formBpm" :style="{ marginRight: showFlowSubmitButton?'10px':'0px' }" @click="onPrint">打印</a-button>
        <a-button @click="submitForm('submit')" v-if="showFlowSubmitButton">提 交</a-button>
      </a-col>
    </a-row>
    <ErpFinanceProjectProgressPaymentApplicationBillFormPrint v-if="formBpm" class="print_style" id="projectpayContent" :formData="model" :formDataHistory="formData"></ErpFinanceProjectProgressPaymentApplicationBillFormPrint>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import moment from 'moment'
  import { mapGetters } from 'vuex'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectContractManagerInput from '@/views/scy/components/SelectContractManagerInput'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectProjectInput from '@/views/scy/components/SelectProjectInput'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import CommonFlowList from '@/views/components/CommonFlowList.vue';
  import Print from '@/utils/print/printarea.js';
  import ErpFinanceProjectProgressPaymentApplicationBillFormPrint from './ErpFinanceProjectProgressPaymentApplicationBillFormPrint' // 打印
  import {  initDictOptions,filterMultiDictText } from '@/components/dict/JDictSelectUtil'
  import SelectSupplierInput from '@/views/scy/components/selectSupplier/SelectSupplierInput.vue'

  export default {
    name: 'ErpFinanceProjectProgressPaymentApplicationBillForm',
    components: {
      SelectCompanyInput,
      SelectUserByDep,
      SelectTeamInput,
      SelectContractManagerInput,
      SelectCustomerInput,
      SelectProjectInput,
      CommonFlowList,
      ErpFinanceProjectProgressPaymentApplicationBillFormPrint,
      SelectSupplierInput,
      ErpProjectReportModal: () => import('@/views/erp/report/project/modules/ErpProjectReportModal')
    },
    props: {
      // 流程表单data
      formData: {
        type: Object,
        default: () => {},
        required: false
      },
      // 表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    watch: {
      activeKey(key) {
        this.$forceUpdate()
      }
    },
    data () {
      return {
        model: {
          ownerInfo: {
            isRelConllectionContract: '1'
          },
          constructInfo: {
            isRelPaymentContract: '1'
          }
         },
         projectType: '2',
         isSpecialDisabled: false,
         projectPaidTotalAmount: 0, // 项目累计金额
         paidTotalAmount: 0, // 施工方累计金额
         constructTotalAmount: 0, // 所有施工方调整后合同金额累计
        labelCol: {
        },
        activeKey: ['1', '2', '3', '4'],
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
          bpmBizTitle: [
              { required: true, message: '请输入流程标题!' }
          ],
          applicant: [
              { required: true, message: '请输入申请单位!' }
          ],
          projectId: [
              { required: true, message: '请选择关联项目!' }
          ],
          isAdvance: [
              { required: true, message: '请选择是否垫付工程款!' }
          ],
          projectPurchaseCategories: [
            { required: true, message: '请选择采购类别!' }
          ],
          ownerInfo: {
            isRelConllectionContract: [
                { required: true, message: '请选择是否关联收款合同!' }
            ],
            conllectionContractId: [
              { required: true, message: '请选择收款合同!' }
            ],
            ownerAdjustedContractAmount: [
              { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入建设单位调整后合同金额!' }
            ],
            ownerReturnedAmount: [
                { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入建设单位已汇款金额!' }
            ]
          },
          constructInfo: {
            isRelPaymentContract: [
              { required: true, message: '请选择是否关联付款合同!' }
            ],
            paymentContractId: [
              { required: true, message: '请选择付款合同!' }
            ],
            constructionAdjustedContractAmount: [
                { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入施工方调整后合同金额!' }
            ],
            isSpecialInvoice: [
                { required: true, message: '请选择发票类型!' }
            ],
            reimbursementAmount: [
                { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入报销金额!' }
            ],
            payMode: [
                { required: true, message: '请选择付款方式!' }
            ],
            payeeName: [
                { required: true, message: '请选择收款单位!' }
            ],
            purchaseCategories: [
                { required: true, message: '请选择采购类别!' }
            ],
            currentPaymentAmount: [
                { required: true, pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入本次支付金额!' }
            ],
            invoicedTotalAmount: [
                { required: false, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!' }
            ]
          }

        },
        url: {
          queryProjectReportById: '/api/erp/project/report/erpProjectReport/queryById',
          findInvoiceAmountCount: '/api/erp/finance/invoice/erpFinanceInvoiceApplicationBill/findInvoiceAmountCount',
          queryLastApplyByContractId: '/api/erp/omc/contractmanager/erpOmcContractManager/queryLastApplyByContractId',
          queryBillByProject: '/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/queryByProject',
          queryBillByContract: '/api/erp/finance/projectProgress/erpFinanceProjectConstructionPartyInfo/queryBillByContract',
          getBillNo: '/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/getBillNo',
          add: '/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/add',
          edit: '/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/edit',
          queryById: '/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/queryById'
        },
        dictOptions:[],
      }
    },
    computed: {
      formDisabled() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return true
          }
        }
        return false
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      // 如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      // 字典
      this.initDicts()
    },
    methods: {
      goProjectDetail(record) {
        const that = this
        this.$nextTick(() => {
          getAction(this.url.queryProjectReportById, { id: record.projectId }).then((res) => {
            if (res.success) {
              const model = res.result
              that.$refs.projectReport.getDetail(model);
              that.$refs.projectReport.title = '项目一览表';
            }
          })
        })
      },
      // 切换方式
      radioModeChange(data) {
        if (data) {
         this.$set(this.model.constructInfo, 'accountNo', null)
          this.$set(this.model.constructInfo, 'bankName', null)
        }
      },
      // 修改按钮
      radioChange (item) {
        if (item == 1) { // 建筑方信息
          if (this.model.ownerInfo.isRelConllectionContract == '1') {
            this.$refs.form.clearValidate('ownerInfo.conllectionContractId');
          } else {
            this.validatorRules.ownerInfo.conllectionContractId = [
              { required: true, message: '请选择收款合同!' }
            ]
          }
          this.$set(this.model.ownerInfo, 'conllectionContractId', null);
          this.$set(this.model.ownerInfo, 'ownerContractAmount', '');
        } else {
          if (this.model.constructInfo.isRelPaymentContract == '1') {
            this.$refs.form.clearValidate('constructInfo.paymentContractId');
          } else {
            this.validatorRules.constructInfo.paymentContractId = [
              { required: true, message: '请选择付款合同!' }
            ]
          }
          this.$set(this.model.constructInfo, 'paymentContractId', null);
          this.$set(this.model.constructInfo, 'constructionContractAmount', '');
        }
      },
      // 税
      inputTaxAmount() {
        let constructInfo = this.model.constructInfo;
        if (constructInfo.reimbursementAmount || constructInfo.reimbursementAmount==0) {
          if (constructInfo.taxAmount || constructInfo.taxAmount==0) {
            constructInfo.untaxedAmount = (constructInfo.reimbursementAmount - constructInfo.taxAmount).toFixed(2)
          }
        }
      },
      // 发票类型
      isSpecial() {
        if (this.model.constructInfo.isSpecialInvoice == 0) { // 不是专票
            this.$set(this.model.constructInfo, 'taxAmount', 0);
            this.isSpecialDisabled = true;
            this.inputTaxAmount();
            this.$refs.form.clearValidate('constructInfo.taxAmount');
            this.$set(this.validatorRules.constructInfo, 'taxAmount', [{ required: false, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!' }]);
        } else {
          this.isSpecialDisabled = false;
          this.$refs.form.clearValidate('constructInfo.taxAmount');
          this.$set(this.validatorRules.constructInfo, 'taxAmount', [{ required: true, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!' }]);
        }
      },
      // 汇款金额判断
      inputReturnedAmount() {
        let ownerAdjustedContractAmount = this.model.ownerInfo.ownerAdjustedContractAmount; // 调整金额
        let ownerReturnedAmount = this.model.ownerInfo.ownerReturnedAmount; // 回款金额
        if (!!ownerAdjustedContractAmount && !!ownerReturnedAmount) {
           this.$set(this.model.ownerInfo, 'ownerReturnedRate', ((ownerReturnedAmount / ownerAdjustedContractAmount).toFixed(4) * 100).toFixed(2));
        }
      },
      // 修改本次支付金额
      inputAmount() {
        let currentPaymentAmount = this.model.constructInfo.currentPaymentAmount; // 本次支付金额
        let constructionAdjustedContractAmount = this.model.constructInfo.constructionAdjustedContractAmount; // 合同调整金额
        let paidTotalAmount = this.paidTotalAmount; // 累计支付金额
        let currentPaymentRate = ((currentPaymentAmount / constructionAdjustedContractAmount).toFixed(4) * 100).toFixed(2) // 本次支付比例
        let constructTotalAmount = this.constructTotalAmount != 0 ? this.constructTotalAmount : constructionAdjustedContractAmount;// 所有施工方金额
        let projectPaidTotalAmount = this.projectPaidTotalAmount; // 项目总支付金额
        if ((!!projectPaidTotalAmount || projectPaidTotalAmount == 0) && !!currentPaymentAmount) {
          this.$set(this.model, 'projectPaidTotalAmount', (projectPaidTotalAmount + currentPaymentAmount).toFixed(2));
          if (constructTotalAmount) {
            this.$set(this.model, 'projectPaidRate', ((((projectPaidTotalAmount + currentPaymentAmount) / constructTotalAmount).toFixed(4) * 100)).toFixed(2));
          }
        }
        if ((!!paidTotalAmount || paidTotalAmount == 0) && !!currentPaymentAmount) {
          console.log(paidTotalAmount + currentPaymentAmount);
          this.$set(this.model.constructInfo, 'paidTotalAmount', (paidTotalAmount + currentPaymentAmount).toFixed(2));
          if (constructionAdjustedContractAmount) {
             this.$set(this.model.constructInfo, 'paidTotalRate', ((((paidTotalAmount + currentPaymentAmount) / constructionAdjustedContractAmount).toFixed(4) * 100)).toFixed(2));
          }
        }
        if (!!constructionAdjustedContractAmount && !!currentPaymentAmount) {
          this.$set(this.model.constructInfo, 'currentPaymentRate', currentPaymentRate);
        }
      },
      // 获取编号
      getBillNo() {
        let that = this;
        getAction(this.url.getBillNo, {}).then((res) => {
          if (res.success) {
            let data = res.result.records || res.result
            this.$set(this.model, 'billNo', data);
            this.$set(this.model, 'bpmBizTitle', this.userInfo().employee.name + '-' + data);
          }
        })
      },
      // 获取同等项目下的单据信息
      queryBillByProject(data) {
        if (data) {
          this.$set(this.model, 'applicant', data.choseObj[0].signUnit)
          this.$set(this.model, 'projectYear', data.choseObj[0].year)
          this.$set(this.model, 'projectBusiTeamId', data.choseObj[0].teamName)
          // 判断是否关联收款合同
          if (this.model.ownerInfo.isRelConllectionContract == '0') {
              this.$set(this.model.ownerInfo, 'ownerAdjustedContractAmount', null);
              this.$set(this.model.ownerInfo, 'ownerReturnedAmount', null);
              this.$set(this.model.ownerInfo, 'ownerReturnedRate', '');
          } else {
            if (this.model.ownerInfo.conllectionContractId) {
              this.$set(this.model.ownerInfo, 'conllectionContractId', null);
              this.$set(this.model.ownerInfo, 'ownerContractAmount', null);
            }
              this.$set(this.model.ownerInfo, 'ownerAdjustedContractAmount', null);
              this.$set(this.model.ownerInfo, 'ownerReturnedAmount', null);
              this.$set(this.model.ownerInfo, 'ownerReturnedRate', '');
          }
          // 判断是否关联付款合同
          if (this.model.constructInfo.isRelPaymentContract == '0') {
              this.$set(this.model.constructInfo, 'constructionAdjustedContractAmount', null);
              this.$set(this.model.constructInfo, 'invoicedTotalAmount', null);
              this.$set(this.model.constructInfo, 'paidAmount', null);
              this.$set(this.model.constructInfo, 'isSpecialInvoice', null);
              this.$set(this.model.constructInfo, 'reimbursementAmount', null);
              this.$set(this.model.constructInfo, 'taxAmount', null);
              this.$set(this.model.constructInfo, 'untaxedAmount', null);
              this.$set(this.model.constructInfo, 'currentPaymentAmount', null);
              this.$set(this.model.constructInfo, 'currentPaymentRate', '');
              this.$set(this.model.constructInfo, 'paidTotalAmount', null);
              this.$set(this.model.constructInfo, 'paidTotalRate', '');
              this.$set(this.model.constructInfo, 'payMode', null);
              this.$set(this.model.constructInfo, 'payeeName', null);
              this.$set(this.model.constructInfo, 'bankName', '');
              this.$set(this.model.constructInfo, 'accountNo', '');
          } else {
            if (this.model.constructInfo.paymentContractId) {
              this.$set(this.model.constructInfo, 'paymentContractId', null);
              this.$set(this.model.constructInfo, 'constructionContractAmount', null);
            }
            this.$set(this.model.constructInfo, 'constructionAdjustedContractAmount', null);
              this.$set(this.model.constructInfo, 'invoicedTotalAmount', null);
              this.$set(this.model.constructInfo, 'paidAmount', null);
              this.$set(this.model.constructInfo, 'isSpecialInvoice', null);
              this.$set(this.model.constructInfo, 'reimbursementAmount', null);
              this.$set(this.model.constructInfo, 'taxAmount', null);
              this.$set(this.model.constructInfo, 'untaxedAmount', null);
              this.$set(this.model.constructInfo, 'currentPaymentAmount', null);
              this.$set(this.model.constructInfo, 'currentPaymentRate', '');
              this.$set(this.model.constructInfo, 'paidTotalAmount', null);
              this.$set(this.model.constructInfo, 'paidTotalRate', '');
              this.$set(this.model.constructInfo, 'payMode', null);
              this.$set(this.model.constructInfo, 'payeeName', null);
              this.$set(this.model.constructInfo, 'bankName', '');
              this.$set(this.model.constructInfo, 'accountNo', '');
          }

          // if(data.cutStatus!="2"){
          //   this.$message.error('请选择立项项目！');
          //   this.$set(this.model,'projectId',null);
          //   this.$set(this.model,'projectYear','')
          //   this.$set(this.model,'projectBusiTeamId','')
          //   this.$set(this.model,"projectPaidTotalAmount",0);
          //   return ;
          // }else{
          //   this.$set(this.model,'projectYear',data.choseObj[0].year_dictText)
          //   this.$set(this.model,'projectBusiTeamId',data.choseObj[0].teamName)
          // }
        }
        let that = this;
        getAction(this.url.queryBillByProject, { 'projectId': this.model.projectId }).then((res) => {
          if (res.success) {
            let data = res.result
            if (data) {
              this.$set(this.model, 'projectPaidTotalAmount', data.projectTotalAmount);
              this.projectPaidTotalAmount = data.projectTotalAmount;
              this.constructTotalAmount = data.constructAmount;
            }
          }
        })
        this.inputAmount();
      },
      // 获取收款单位信息
      getCustomerInfo(data) {
        if (data) {
          this.$set(this.model.constructInfo, 'accountNo', data[0].bankAccountNo)
          this.$set(this.model.constructInfo, 'bankName', data[0].bankName)
        }
      },
      // 获取收款合同信息
      getCollectContractInfo(data) {
        if (data) {
          console.log(data);
          this.$set(this.model.ownerInfo, 'ownerAdjustedContractAmount', data[0].bchte)
          this.$set(this.model.ownerInfo, 'ownerReturnedAmount', data[0].realCollectionPaymentMoney)
          // 获取之前审核的金额
          getAction(this.url.queryLastApplyByContractId, { 'contractNo': data[0].htbh }).then((res) => {
            if (res.success) {
              let resdata = res.result
              if (resdata) {
                this.$set(this.model.ownerInfo, 'ownerContractAmount', resdata.bchte)
              } else {
                this.$set(this.model.ownerInfo, 'ownerContractAmount', data[0].bchte)
              }
            } else {
              this.$set(this.model.ownerInfo, 'ownerContractAmount', data[0].bchte)
            }
          })
          this.inputReturnedAmount();
        }
      },
      // 获取同等合同下的单据信息
      getContractInfo(data) {
        if (data) {
          this.$set(this.model.constructInfo, 'constructionAdjustedContractAmount', data[0].bchte)
        }
        let that = this;
        // 获取本合同的累计金额
        getAction(this.url.queryBillByContract, { 'contractId': this.model.constructInfo.paymentContractId }).then((res) => {
          if (res.success) {
            let data = res.result
            this.$set(this.model.constructInfo, 'paidAmount', data);
            if (this.model.constructInfo.currentPaymentAmount) {
              this.$set(this.model.constructInfo, 'paidTotalAmount', (data + this.model.constructInfo.currentPaymentAmount).toFixed(2));
            } else {
              this.$set(this.model.constructInfo, 'paidTotalAmount', data);
            }
            this.paidTotalAmount = data;
          }
        })
        // 获取累计开票金额
        getAction(this.url.findInvoiceAmountCount, { 'projectContract': this.model.constructInfo.paymentContractId }).then((res) => {
          if (res.success) {
            let data = res.result
            if (data) {
              this.$set(this.model.constructInfo, 'invoicedTotalAmount', data);
            } else {
              this.$set(this.model.constructInfo, 'invoicedTotalAmount', 0);
            }
          }
          // 获取付款后金额
          getAction(this.url.queryLastApplyByContractId, { 'contractNo': data[0].htbh }).then((res) => {
            if (res.success) {
              let resdata = res.result
              if (resdata) {
                this.$set(this.model.constructInfo, 'constructionContractAmount', resdata.bchte)
              } else {
                this.$set(this.model.constructInfo, 'constructionContractAmount', data[0].bchte)
              }
            } else {
              this.$set(this.model.constructInfo, 'constructionContractAmount', data[0].bchte)
            }
          })
        })
        this.inputAmount();
      },
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
        this.getBillNo();
        this.model.proposer = this.userInfo().empId;
        this.model.proposerDeptId = this.userInfo().deptId;
        // this.model.applicant = this.userInfo().employee.orgDepart; //当前登录人单位
        this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss');
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        // 编辑时重新获取支付累计金额
        if (this.model.constructInfo.paymentContractId) {
          // 获取本合同的累计金额
          getAction(this.url.queryBillByContract, { 'contractId': this.model.constructInfo.paymentContractId }).then((res) => {
            if (res.success) {
              let data = res.result
              this.paidTotalAmount = data;
            }
          })
        }
        // 编辑时重新获取项目累计支付金额
        if (this.model.projectId) {
          getAction(this.url.queryBillByProject, { 'projectId': this.model.projectId }).then((res) => {
          if (res.success) {
            let data = res.result
            if (data) {
              this.projectPaidTotalAmount = data.projectTotalAmount;
              this.constructTotalAmount = data.constructAmount;
            }
          }
        })
        }

         // 加载关联的项目
         if (this.formDisabled && this.model.projectId) {
          getAction(this.url.queryProjectReportById, { id: this.model.projectId }).then(res => {
              this.$set(this.model, 'projectName', res.result.projectName);
          }).catch(e => {
            console.log(e)
          })
        }
        this.isSpecial()
      },
      // 渲染流程表单数据
      showFlowData() {
        if (this.formBpm === true) {
          let params = { id: this.formData.dataId };
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result);
            }
          });
        }
      },

      submitForm(type) {
        const that = this;
        if (type == 'draft') {
          // 代表保存草稿不做校验
          this.submit(type)
        } else if (type == 'submit') {
          // 触发表单验证
          this.$refs.form.validate(valid => {
            if (valid) {
              that.submit(type)
            } else {
              that.$emit('unButtonDisable')
            }
          })
        } else {
          console.log('传参数，保存：draft，提交：submit')
        }
      },
      submit (type) {
        const that = this;
        let teamName = filterMultiDictText(this.dictOptions['teamManager'], that.model.projectBusiTeamId + '')
        let createTime = this.getCreateTime()
        // 迭代16.7 取费用所属团队-申请人-申请日期（年月日）-金额（取报销金额）
        that.model.bpmBizTitle = (that.model.projectBusiTeamId ? teamName + ' - ' : '') + that.userInfo().empName + ' - ' + createTime + (that.model.constructInfo.reimbursementAmount ? ' - ' + that.model.constructInfo.reimbursementAmount : '') // 标题
        that.confirmLoading = true;
        let httpurl = '';
        let method = '';
        if (!this.model.id) {
          httpurl += this.url.add;
          method = 'post';
        } else {
          httpurl += this.url.edit;
          method = 'put';
        }
        httpAction(httpurl, this.model, method).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            if (method == 'post') {
              that.model.id = res.result;
            }
            that.$emit('ok', type, that.model);
          } else {
            that.$message.warning(res.message);
            that.$emit('unButtonDisable');
          }
        }).catch((error) => {
          that.$emit('unButtonDisable', error);
        }).finally(() => {
          that.confirmLoading = false;
        })
      },

      // 点击打印
      onPrint () {
        let print = new Print({
          el: '#projectpayContent',
          popTitle: '工程进度款支付申请详情 · 禾美环保',
          endCallback() {

          }
        })
      },
      initDicts(){
        initDictOptions('erp_omc_team_manager,alias_name,id').then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'teamManager', res.result)
          }
        })
      },
      getCreateTime() {
        let createTime = this.model.createTime;
        console.log(createTime)
        let date = createTime.substring(0,10)
        return date.substring(0,4) +'年'+ date.substring(5,7)+'月' + date.substring(8,10)+'日';
      }
    }
  }
</script>
<style>
  @import "../../../../../utils/print.css";
  .print_style{
  position: absolute;
  top: 0;
  z-index: -1111;
}
</style>
